package com.david.client.ui.usermanager;

import com.david.client.dto.UserDTO;
import com.david.client.rpc.IUserManager;
import com.david.client.ui.common.RoundCornerBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class NewUserForm extends Composite {

//	private FlexTable tbl;

	private final TextBox userID = new TextBox();

	private final TextBox firstName = new TextBox();

	private final TextBox middleName = new TextBox();

	private final TextBox lastName = new TextBox();

	private final PasswordTextBox passwd2 = new PasswordTextBox();

	private final PasswordTextBox passwd = new PasswordTextBox();

	public NewUserForm() {
		RoundCornerBox box = new RoundCornerBox();
		box.setTitle("Add new user");
		box.setContent(getForm());
		box.setStyleName("RoundCornerBox");
		initWidget(box);
	}
	
	private FlexTable getForm(){
		FlexTable tbl = new FlexTable();
		tbl.setWidget(0, 0, new Label("Create New User"));
		tbl.getFlexCellFormatter().setColSpan(0, 0, 2);

		tbl.setWidget(1, 0, new Label("User ID"));

		tbl.setWidget(1, 1, userID);

		tbl.setWidget(2, 0, new Label("Pass Word"));

		tbl.setWidget(2, 1, passwd);

		tbl.setWidget(3, 0, new Label("Pass Word Confirm"));

		tbl.setWidget(3, 1, passwd2);

		tbl.setWidget(4, 0, new Label("First Name"));

		tbl.setWidget(4, 1, firstName);
		tbl.setWidget(4, 2, new Label("Middle Name"));

		tbl.setWidget(4, 3, middleName);

		tbl.setWidget(4, 4, new Label("Last Name"));

		tbl.setWidget(4, 5, lastName);

		Button submit = getSubmitBtn();
		tbl.setWidget(5, 0, submit);
		tbl.getFlexCellFormatter().setColSpan(5, 0, 5);
		
		return tbl;
	}

	private Button getSubmitBtn() {
		Button btn = new Button("Add");

		btn.addClickListener(new ClickListener() {
			public void onClick(Widget arg0) {
				UserDTO user = new UserDTO();
				user.setLoginName(userID.getText());
				user.setFirstName(firstName.getText());
				user.setLastName(lastName.getText());
				user.setPasswd(passwd.getText());
				// user.addRole(new Role)

				IUserManager.Util.getInstance().addNewUser(user,
						new AsyncCallback() {

							public void onFailure(Throwable arg0) {
//								tbl.setWidget(6, 0, new Label(
//										"Failed to add user to system"));
							}

							public void onSuccess(Object arg0) {
//								Integer retVal = (Integer) arg0;
//								Label msg;
//								if (retVal.intValue() == IUserManager.SUCCESS) {
//									msg = new Label("Successfully add new user");
//								} else {
//									msg = new Label(
//											"Failed to add user,error code="
//													+ retVal.intValue());
//								}
//
//								tbl.setWidget(6, 0, msg);

							}

						});
			}

		});

		return btn;

	}
}
